package com.bjfu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjfu.eduservice.client.VodClient;
import com.bjfu.eduservice.entity.EduVideo;
import com.bjfu.eduservice.mapper.EduVideoMapper;
import com.bjfu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjfu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author Guli
 * @since 2020-07-04
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;

    @Override
    @Transactional
    public void removeByCourseId(String courseId) {
        // 1.根据课程id查询所有的视频id
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        // 只查询视频id列
        wrapper.select("video_source_id");
        List<EduVideo> eduVideoList = baseMapper.selectList(wrapper);
        // 2.获取视频id集合
        List<String> videoIds = new ArrayList<>();
        eduVideoList.stream().filter(s->s!=null).forEach(s->{
            String sourceId = s.getVideoSourceId();
            if(!StringUtils.isEmpty(sourceId)){
                videoIds.add(sourceId);
            }
        });
        /*for (EduVideo video : eduVideoList) {
            String sourceId = video.getVideoSourceId();
            if(!StringUtils.isEmpty(sourceId)){
                videoIds.add(sourceId);
            }
        }*/

        // 3.根据视频Id批量删除视频
        if(videoIds.size() > 0){
            vodClient.deleteBatch(videoIds);
        }

        // 删除小节
        baseMapper.delete(wrapper);
    }

    public static void main(String[] args) {
        List<EduVideo> list = new ArrayList<>();
        list.add(null);
        System.out.println(list);
    }
}


